local StoryParser = {}

local prefix = "gameLogic.story.behavior."

function StoryParser.parse(storyName, actuator)
    local sotyRes = G_ResourceManager:loadPrefabs("Prefabs/Story/" .. storyName)
    local resTrans = sotyRes.transform
    if resTrans.childCount < 1 then
        return
    end
    -- 剧情资源第一个孩子就是剧情根节点
    local params = resTrans:GetChild(0):GetComponent(typeof(CS.StoryParams))
    local root = require(prefix .. params.comName).new()
    root:setActuator(actuator)
    root:setParams(params)
    StoryParser._constructStoryTree(root, resTrans:GetChild(0), actuator)
    return root
end

function StoryParser.parseObj(obj,actuator)
    local resTrans = obj.transform
    if resTrans.childCount < 1 then
        return
    end
    -- 剧情资源第一个孩子就是剧情根节点
    local params = resTrans:GetChild(0):GetComponent(typeof(CS.StoryParams))
    local root = require(prefix .. params.comName).new()
    root:setActuator(actuator)
    root:setParams(params)
    StoryParser._constructStoryTree(root, resTrans:GetChild(0), actuator)
    return root
end

function StoryParser._constructStoryTree(parent, transform, actuator)
    for i = 0, transform.childCount - 1 do
        local params = transform:GetChild(i):GetComponent(typeof(CS.StoryParams))
        if params then
            params.pos = params.transform.position
            params.eulerAngles = params.transform.eulerAngles
            local treeNode = require(prefix .. params.comName).new()
            treeNode:setActuator(actuator)
            treeNode:setParams(params)
            parent:addChild(treeNode)
            StoryParser._constructStoryTree(treeNode, transform:GetChild(i), actuator)
        end
    end
end

return StoryParser
